//
//  LXAppointmemtListViewController.m
//  lixing
//
//  Created by bancheng on 2022/6/25.
//

#import "LXAppointmemtViewController.h"
#import "LXAppointmemtListView.h"
#import "LXAppointmemtDetailVC.h"
#import "LXAppointmentModel.h"
#define lineWidth 18

@interface LXAppointmemtViewController ()<UIScrollViewDelegate,LXDidSelectRowDelegate>
@property (nonatomic) UIButton *topBtn;
@property (nonatomic) UIView *topLine;
@property (nonatomic) UIView *topView;
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) NSMutableArray *titlesAry;
@end



@implementation LXAppointmemtViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self layoutCustomNav:self.view  title:@"预约管理"];
    self.lineView.hidden = YES;
    self.titlesAry = [NSMutableArray array];
    
        
    
    NSArray *ary = @[@"全部",@"已预约",@"待确认",@"已取消"];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopHeight + 5, ScreenWidth, 30)];
    self.topView = topView;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topView.Y_height, ScreenWidth, kScreenHeight - topView.Y_height)];
    [self.view addSubview:scrollView];
    [scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    self.scrollView = scrollView;
    scrollView.contentSize = CGSizeMake(ary.count * ScreenWidth, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    
    
    
   
    [self.view addSubview:topView];
    CGFloat X = 0;
    CGFloat W = ScreenWidth / ary.count;
    for (int i = 0; i < ary.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, W, 20)];
        [btn setTitle:ary[i] forState:UIControlStateNormal];
        [btn setTitleColor:ColorWith333333 forState:UIControlStateNormal];
        [btn setTitleColor:ColorWith3C6CFC forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.tag = i + 100;
        [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:btn];
        if (i == self.selectIndex) {
            btn.selected = YES;
            btn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
            self.topBtn = btn;
            self.topLine = [[UIView alloc] initWithFrame:CGRectMake(X + (btn.width - lineWidth)/2, topView.height-4, lineWidth, 2)];
            self.topLine.backgroundColor = ColorWith3C6CFC;
            [topView addSubview:self.topLine];
        }
        
        [self.titlesAry addObject:btn];
        
        LXAppointmemtListView *vc = [[LXAppointmemtListView alloc] initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, self.scrollView.height)];
        vc.selectIndex = i;
        vc.delegate = self;
        vc.tag = btn.tag;
        [self.scrollView setContentOffset:CGPointMake(self.selectIndex * self.scrollView.bounds.size.width, 0)];
        [scrollView addSubview:vc];
        
        X = btn.X_width;
    }
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUnCount:) name:@"setUnCountNoti" object:nil];
    
}
-(void)setUnCount:(NSNotification *)info{
    NSDictionary *dic =  info.object;
    
    for (int i = 0; i < self.titlesAry.count; i++) {
        
        UIButton *btn = self.titlesAry[i];
        if (btn.tag - 100 == [dic[@"status"] intValue]) {
            [btn setTitle:dic[@"title"] forState:UIControlStateNormal];
            return;
        }
    }
}
#pragma mark -- 点击cell
- (void)didselectRowWithModel:(LXAppointmentModel *)model{
    
    LXAppointmemtDetailVC *vc = [[LXAppointmemtDetailVC alloc] init];
    vc.Id = model.ID;
    vc.fromVc = NSStringFromClass([self class]);
    [self pushToNextController:vc];
    
}
#pragma mark -- 点击topView按钮
-(void)clickBtnAction:(UIButton *)sender{
    if (sender.isSelected) return;
    sender.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    sender.selected = YES;
    
    self.topBtn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    self.topBtn.selected = NO;
    self.topLine.X = sender.X + (sender.width - lineWidth)/2;
    self.topBtn = sender;
    NSInteger pageCount = sender.tag - 100;
    [self.scrollView setContentOffset:CGPointMake(pageCount * self.scrollView.bounds.size.width, 0)];

    LXAppointmemtListView *view = [self.view viewWithTag:sender.tag];
    [view loadData];
   
    self.selectIndex = pageCount;
    
    
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

    int pageCount = scrollView.contentOffset.x/scrollView.bounds.size.width;
    
    LXAppointmemtListView *view = [self.view viewWithTag:pageCount + 100];
    [view loadData];
    self.selectIndex = pageCount;
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.topBtn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    self.topBtn.selected = NO;
    // 用contentOffset来计算当前的页码
    int pageCount = scrollView.contentOffset.x/scrollView.bounds.size.width;
    UIButton *btn = [self.topView viewWithTag:pageCount + 100];
    self.topLine.X = btn.X + (btn.width - lineWidth)/2;
    btn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    btn.selected = YES;
    self.topBtn = btn;
    
}

@end
